package Modelo;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class FileXML {
    
    private String name;
    private Long size;
    private Long modify;
    private String xml;
    
    public FileXML()
    {
        this.name = "";
        this.size = 0L;
        this.modify = 0L;
        this.xml = null;
    }

    
    public FileXML( String name, Long size, Long modify, String x ) {
       
        this.name = name;
        this.size = size;
        this.modify = modify;
        this.xml = x;
    }
    
    public FileXML( File file ) {
        
        this.name = file.getName();
        this.size = file.getTotalSpace();
        this.modify = file.lastModified();
        this.xml = Util.readTextFile(file.getAbsolutePath());
    }
    
    public void save(){
        String fileName = Util.selectPath(0);
        
        try
        {
            OutputStream fout= new FileOutputStream(fileName);
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            DOMSource source = new DOMSource(this.toXml());
            StreamResult result = new StreamResult(fout);
            transformer.transform(source, result);
        }
        catch( Exception e ){  };
    }

    public String getName() {
        return name;
    }

    public void setName(String nombre) {
        this.name = nombre;
    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    public Long getModify() {
        return modify;
    }

    public void setModify(Long modify) {
        this.modify = modify;
    }
    
    public String getXml() {
        return xml;
    }

    public void setXml(String x) {
        this.xml = x;
    }
    
   public Node toXml(){
      try
      {
          DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
          DocumentBuilder parser = fact.newDocumentBuilder();
          Document doc = parser.newDocument();

          return toXml( doc );
      }
      catch( Exception ex )
      {
         return null;
      }
  }
    
    public Node toXml( Document doc ){
        
      try {
      Node root = doc.createElement("file");
      //doc.appendChild(root);

      Node nombre = doc.createElement("name");
      nombre.appendChild(doc.createTextNode(this.name));
      root.appendChild(nombre);
      
      Node tama = doc.createElement("size");
      String tamSize = ""+size;
      tama.appendChild(doc.createTextNode(tamSize));
      root.appendChild(tama);
      
      Node modi = doc.createElement("modify");
      String modModi = ""+modify;
      modi.appendChild(doc.createTextNode(modModi));
      root.appendChild(modi);

      Node cData = doc.createElement("xml");
      Node xmlString = doc.createCDATASection(xml);
      cData.appendChild(xmlString);
      root.appendChild(cData);
      
      return root;

    } catch (Exception ex) {
      System.err.println("+============================+");
      System.err.println("|        XML Error           |");
      System.err.println("+============================+");
      System.err.println(ex.getClass());
      System.err.println(ex.getMessage());
      System.err.println("+============================+");
      ex.printStackTrace();
      return null;
    }
  } 
    
    public static FileXML fromXml( String str )
  {
        String name = "";
        Long  size, modify;
        
        ByteArrayInputStream is;
        try{
             is = new ByteArrayInputStream( str.getBytes("UTF8") );
        }catch(Exception e){
            return null;
        }
        
        try
        {
            DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = fact.newDocumentBuilder();
            Document doc = parser.parse( is );
            
            return fromXml( doc );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
  }

  public static FileXML fromXml( Document doc )
  {
        String name = "";
        Long size=0L;
        Long modify=0L;
        Ingredient ingredient=null;
        
        try
        {
            NodeList nodes = doc.getElementsByTagName( "name" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                
                name = node.getFirstChild().getNodeValue();
            }
            
            nodes = doc.getElementsByTagName( "size" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                
                String tam = node.getFirstChild().getNodeValue();
                size = Long.parseLong(tam);
            }
            
            nodes = doc.getElementsByTagName( "modify" );
            if( nodes.getLength() == 1 )
            {
                Node node = nodes.item( 0 );
                
                String modi = node.getFirstChild().getNodeValue();
                modify = Long.parseLong(modi);
            }
            
            nodes = doc.getElementsByTagName("ingredient");
            Node node = nodes.item(0);
            String ing = node.getFirstChild().getNodeValue();
            
            return new FileXML( name, size, modify, ing );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
  }
}

